﻿using System;
using System.Collections.Generic;
using System.Linq;
using TavDom.Library.Business.Interfaces;
using TavDom.Library.Core.Utilities.Search;
using TavDom.Library.DataAccess.Abstract;
using TavDom.Library.Entities.ComplexTypes;
using TavDom.Library.Entities.Concrete;

namespace TavDom.Library.Business.Concrete
{
    public class UserManager : IUserService
    {
        private readonly IUserDal _entityDal;
        public UserManager(IUserDal entityDal)
        {
            _entityDal = entityDal;
        }
        public IQueryable<User> GetAll()
        {
            return _entityDal.GetAll();
        }

        public IQueryable<User> GetAllWithUsers()
        {
            return _entityDal.GetAllWithUsers();
        }

        public User Add(User entity)
        {
            _entityDal.Add(entity);
            return entity;
        }

        public User Get(User entity)
        {
            return _entityDal.Get(entity);
        }

        public User GetById(int id)
        {
            return _entityDal.GetById(id);
        }

        public User Update(User entity)
        {
            _entityDal.Update(entity);
            return entity;
        }

        public void Delete(User entity)
        {
            _entityDal.Delete(entity);
        }

       

        public IQueryable<User> Search(SearchEntity<User> entity)
        {
            var filters = new List<SearchFilter>();

            if (!String.IsNullOrEmpty(entity.Username))
            {
                filters.Add(
                     new SearchFilter
                     {
                         PropertyName = "Username",
                         Operation = Op.Contains,
                         Value = entity.Username
                     });
            }

            if (!String.IsNullOrEmpty(entity.Name))
            {
                filters.Add(
                     new SearchFilter
                     {
                         PropertyName = "Name",
                         Operation = Op.Contains,
                         Value = entity.Name
                     });
            }
            if (!String.IsNullOrEmpty(entity.Surname))
            {
                filters.Add(
                     new SearchFilter
                     {
                         PropertyName = "Surname",
                         Operation = Op.Contains,
                         Value = entity.Surname
                     });
            }

            if (!String.IsNullOrEmpty(entity.Email))
            {
                filters.Add(
                     new SearchFilter
                     {
                         PropertyName = "Email",
                         Operation = Op.Contains,
                         Value = entity.Email
                     });
            }

            if (entity.CreateDateBegin != null)
            {
                filters.Add(
                    new SearchFilter
                    {
                        PropertyName = "CreateDate",
                        Operation = Op.GreaterThanOrEqual,
                        Value = entity.CreateDateBegin
                    });
            }

            if (entity.CreateDateEnd != null)
            {
                filters.Add(
                    new SearchFilter
                    {
                        PropertyName = "CreateDate",
                        Operation = Op.LessThanOrEqual,
                        Value = entity.CreateDateEnd
                    });
            }

            if (entity.UpdateDateBegin != null)
            {
                filters.Add(
                    new SearchFilter
                    {
                        PropertyName = "UpdateDate",
                        Operation = Op.GreaterThanOrEqual,
                        Value = entity.UpdateDateBegin
                    });
            }

            if (entity.UpdateDateEnd != null)
            {
                filters.Add(
                    new SearchFilter
                    {
                        PropertyName = "UpdateDate",
                        Operation = Op.LessThanOrEqual,
                        Value = entity.UpdateDateEnd
                    });
            }


            if (entity.IsApproved == 0)
            {
                filters.Add(
                    new SearchFilter
                    {
                        PropertyName = "IsApproved",
                        Operation = Op.Equals,
                        Value = false
                    });
            }
            if (entity.IsApproved == 1)
            {
                filters.Add(
                    new SearchFilter
                    {
                        PropertyName = "IsApproved",
                        Operation = Op.Equals,
                        Value = true
                    });
            }

            return _entityDal.Search(filters);
        }
    }
}
